package com.ruoyi.web.controller.user;

import java.util.List;

import com.ruoyi.circle.service.ICircleBasicInfoService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.tag.domain.TagsInfo;
import com.ruoyi.tag.service.ITagsInfoService;
import com.ruoyi.system.dao.IUserTagsRelDao;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.UserTagsRel;
import com.ruoyi.system.service.IUserTagsRelService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户标签关系Controller
 *
 * @author 区嘉盛
 */
@RestController
@RequestMapping("/user/tags")
@Tag(name = "【用户标签关系】管理")
public class UserTagsRelController extends BaseController
{
    @Autowired
    private IUserTagsRelService userTagsRelService;

    @Autowired
    private ITagsInfoService tagsInfoService;

    @Autowired
    private IUserTagsRelDao userTagsRelDao;

    @Autowired
    private ICircleBasicInfoService circleBasicInfoService;

    /**
     * 查询用户标签关系列表
     */
    @Operation(summary = "查询用户标签关系列表")
    @PreAuthorize("@ss.hasPermi('user:tags:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserTagsRel userTagsRel)
    {
        startPage();
        List<UserTagsRel> list = userTagsRelService.selectUserTagsRelList(userTagsRel);
        return getDataTable(list);
    }

    /**
     * 获取用户标签关系详细信息
     */
    @Operation(summary = "获取用户标签关系详细信息")
    @PreAuthorize("@ss.hasPermi('user:tags:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userTagsRelService.selectUserTagsRelById(id));
    }

    /**
     * 获取当前用户的标签列表
     */
    @Operation(summary = "获取当前用户的标签列表")
    @GetMapping("/current")
    public AjaxResult getCurrentUserTags()
    {
        Long userId = SecurityUtils.getUserId();
        List<Long> tagIds = userTagsRelService.selectTagIdsByUserId(userId);
        List<TagsInfo> tagsList = tagsInfoService.selectTagsInfoByIds(tagIds.toArray(new Long[0]));
        return success(tagsList);
    }

    /**
     * 获取指定用户的标签列表
     */
    @Operation(summary = "获取指定用户的标签列表")
    @PreAuthorize("@ss.hasPermi('user:tags:query')")
    @GetMapping("/user/{userId}")
    public AjaxResult getUserTags(@PathVariable("userId") Long userId)
    {
        List<Long> tagIds = userTagsRelService.selectTagIdsByUserId(userId);
        List<TagsInfo> tagsList = tagsInfoService.selectTagsInfoByIds(tagIds.toArray(new Long[0]));
        return success(tagsList);
    }

    /**
     * 新增用户标签关系
     */
    @Operation(summary = "新增用户标签关系")
    @PreAuthorize("@ss.hasPermi('user:tags:add')")
    @Log(title = "用户标签关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserTagsRel userTagsRel)
    {
        return toAjax(userTagsRelService.insertUserTagsRel(userTagsRel));
    }

    /**
     * 更新用户标签
     */
    @Operation(summary = "更新用户标签")
    @PreAuthorize("@ss.hasPermi('user:tags:edit')")
    @Log(title = "用户标签关系", businessType = BusinessType.UPDATE)
    @PutMapping("/update/{userId}")
    public AjaxResult updateUserTags(@PathVariable("userId") Long userId, @RequestBody Long[] tagIds)
    {
        return toAjax(userTagsRelDao.updateUserTags(userId, tagIds));
    }

    /**
     * 更新当前用户标签
     */
    @Operation(summary = "更新当前用户标签")
    @Log(title = "用户标签关系", businessType = BusinessType.UPDATE)
    @PutMapping("/update/current")
    public AjaxResult updateCurrentUserTags(@RequestBody Long[] tagIds)
    {
        Long userId = SecurityUtils.getUserId();
        int result = userTagsRelDao.updateUserTags(userId, tagIds);

        // 如果标签更新成功且有标签被选择，则将用户加入到包含这些标签的圈子中
        if (result > 0 && tagIds != null && tagIds.length > 0) {
            circleBasicInfoService.addUserToCirclesByTags(userId, tagIds);
        }

        return toAjax(result);
    }

    /**
     * 修改用户标签关系
     */
    @Operation(summary = "修改用户标签关系")
    @PreAuthorize("@ss.hasPermi('user:tags:edit')")
    @Log(title = "用户标签关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserTagsRel userTagsRel)
    {
        return toAjax(userTagsRelService.updateUserTagsRel(userTagsRel));
    }

    /**
     * 删除用户标签关系
     */
    @Operation(summary = "删除用户标签关系")
    @PreAuthorize("@ss.hasPermi('user:tags:remove')")
    @Log(title = "用户标签关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userTagsRelService.deleteUserTagsRelByIds(ids));
    }
}